package cn.comtom.ewbs.signature.controller;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.comtom.ewbs.signature.common.enums.BasicError;
import cn.comtom.ewbs.signature.common.vo.request.DataReq;
import cn.comtom.ewbs.signature.common.vo.request.SoftalgCertImportReq;
import cn.comtom.ewbs.signature.common.vo.response.ApiEntityResponse;
import cn.comtom.ewbs.signature.common.vo.response.ApiResponse;
import cn.comtom.ewbs.signature.common.vo.response.ApiResponseBuilder;
import cn.comtom.ewbs.signature.service.impl.SoftAlgServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

//git.comtom.cn:18000/ct-ewbsv2.0/ct-ewbsv2.0.git

@Slf4j
@RestController
@RequestMapping("/softAlg")
@Api(tags = "softAlg签名服务")
public class SoftAlgController extends BaseController{

    @Autowired
    private SoftAlgServiceImpl signatureService;
    
    @Autowired
    private SoftAlgServiceImpl softAlgServiceImpl;
    
    @PostMapping("/getSignByStr")
    @ApiOperation(value = "获取签名", notes = "接收文本字符串")
    public ApiEntityResponse<String> getSignByStream(@RequestBody @Valid DataReq data) {
        try {
        	String sm2ByteSign = softAlgServiceImpl.getSm2ByteSign(data.getData());
			ApiEntityResponse ok = ApiEntityResponse.ok(BasicError.OK_KEY_VALUE);
			ok.setData(sm2ByteSign);
			log.info("生成签名getSign, sign:{}",sm2ByteSign);
			return ok;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ApiResponseBuilder.buildEntityError(BasicError.VERIFY_SIGN_FAILURE);
    }

    @PostMapping(value="/verifySignByStr")
    @ApiOperation(value = "验证签名", notes = "接收文本字符串")
    public ApiResponse verifySignByStream(@RequestBody @Valid DataReq data){
        if(StringUtils.isBlank(data.getSignValue())){
            return ApiResponseBuilder.buildEntityError(BasicError.PARAM_VALID_ERROR);
        }
		try {
			Boolean b = softAlgServiceImpl.verifySm2ByteSign(data.getData(), data.getSignValue());
			log.info("验证签名verifySign,signValue:{},result:{}", data ,data.getSignValue(),b);
			if(b){
	            return ApiResponse.ok();
	        }
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponseBuilder.buildError(BasicError.VERIFY_SIGN_FAILURE.getCode(), e.getMessage());
		}
		return ApiResponseBuilder.buildEntityError(BasicError.VERIFY_SIGN_FAILURE);
    }

    @PostMapping(value="/importCert")
    @ApiOperation(value = "导入信任证书", notes = "导入信任证书")
    public ApiResponse importCert(@RequestBody @Valid SoftalgCertImportReq req){
        if(req==null || StringUtils.isBlank(req.getCertNo()) || StringUtils.isBlank(req.getPublicKey())){
            return ApiResponseBuilder.buildEntityError(BasicError.PARAM_VALID_ERROR);
        }
        try {
        	Boolean b = signatureService.importTrustCert(req);
	        log.info("导入信任证书 result:{}", b);
			if(b){
	            return ApiResponse.ok();
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ApiResponseBuilder.buildEntityError(BasicError.IMPORT_CERT_FAILURE);
    }

}
